import 'package:flutter/material.dart';
import 'package:top_foundation/component/date_picker/widgets/widgets.dart';
import 'package:top_foundation/component/dialog/choose.dart';
import 'package:top_foundation/utils/theme/theme.dart';
import 'package:top_foundation/utils/util.dart';

Future<T?> showActionSheet<T>({
  required List<String> titles,
  String? val,
  final String? title,
  Function(int)? onTap,
}) async {
  if (Get.context == null) {
    return null;
  }
  if (titles.length == 1) {
    val = titles.first;
  }
  return showModalBottomSheet(
      context: Get.context!,
      constraints: BoxConstraints.expand(height: 0.42.sh),
      builder: (context) {
        return Column(
          children: [
            Container(
              height: 50.r,
              alignment: Alignment.center,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  TextButton(
                      onPressed: () {
                        Get.back(result: -1);
                        if (onTap != null) {
                          onTap(-1);
                        }
                      },
                      child: Text(
                        '取消',
                        style: TextStyle(fontSize: 15.sp),
                      )),
                  Text(
                    title ?? '',
                    style: ITheme.text.headlineMedium,
                  ),
                  TextButton(
                      onPressed: () {
                        var idx = titles.indexOf(val ?? '');
                        Get.back(result: idx);
                        if (onTap != null) {
                          onTap(idx);
                        }
                      },
                      style: ButtonStyle(
                          foregroundColor:
                              MaterialStatePropertyAll(ITheme.selectedColor)),
                      child: Text(
                        '确定',
                        style: TextStyle(fontSize: 15.sp),
                      )),
                ],
              ),
            ),
            Expanded(
              child: DateWheelView(
                value: val,
                list: titles,
                isLoop: false,
                valueChanged: (p0) {
                  val = p0;
                },
              ),
            )
          ],
        );
      });
}

Future<List<int>?> showChoosedSheet({
  required List<String> titles,
  List<int>? initialIndexs,
}) async {
  if (Get.context == null) {
    return Future.value([]);
  }
  List<int> choosedIndexs = [];
  return showModalBottomSheet(
    context: Get.context!,
    backgroundColor: Colors.white,
    constraints: const BoxConstraints.expand(),
    builder: (context) {
      return DialogChooseView(
        titles: titles,
        callback: (p0) {
          choosedIndexs = p0;
        },
        initialIndexs: initialIndexs,
        onCancel: Get.back,
        onSure: () => Get.back(result: choosedIndexs),
      );
    },
  );
}
